package com.feeyin.radar.commons;

import java.util.Date;
import org.apache.poi.hssf.usermodel.HSSFCell;

public class ExcelUtils
{
  public static String getStringCellValue(HSSFCell cell)
  {
    String strCell = "";
    if (cell == null) return "";
    switch (cell.getCellType()) {
    case 1:
      strCell = cell.getStringCellValue();
      break;
    case 0:
      strCell = Double.valueOf(cell.getNumericCellValue()).longValue()+"";
      break;
    case 4:
      strCell = String.valueOf(cell.getBooleanCellValue());
      break;
    case 3:
      strCell = "";
      break;
    case 2:
    default:
      strCell = "";
    }

    if ((strCell.equals("")) || (strCell == null)) {
      return "";
    }
    if (cell == null) {
      return "";
    }
    return strCell;
  }

  public static String getDateCellValue(HSSFCell cell)
    throws Exception
  {
    String result = "";
    try {
      int cellType = cell.getCellType();
      if (cellType == 0) {
        Date date = cell.getDateCellValue();
        result = date.getYear() + 1900 + "-" + (date.getMonth() + 1) + 
          "-" + date.getDate();
      } else if (cellType == 1) {
        String date = getStringCellValue(cell);
        result = date.replaceAll("[年月]", "-").replace("日", "").trim();
      } else if (cellType == 3) {
        result = "";
      }
    }
    catch (Exception e) {
      return result = "";
    }
    return result;
  }
}